package com.kurumi.controller;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kurumi.pojo.RespondResult;
import com.kurumi.query.MedicalRecordQuery;
import com.kurumi.service.MedicalRecordPutAwayService;
import com.kurumi.util.DateUtil;
import com.kurumi.util.ExcelUtils;

@Controller
@RequestMapping("/medical_record_put_away")
public class MedicalRecordPutAwayController {

	
	@Autowired
	private MedicalRecordPutAwayService medicalRecordPutAwayService;
	
	@GetMapping("/put_away_page")
	public String putAwayPage(Model model){
		return "put_away/put_away_page";
	}
	
	
	@GetMapping("/ajax_query_put_away_record")
	@ResponseBody
	public RespondResult ajaxQueryPutAwayRecord(MedicalRecordQuery params){
		RespondResult respondResult = null;
		
		try{
			List<Map<String,Object>> medicalRecords = new ArrayList<Map<String,Object>>();
			int count = 0;
			if(!params.querySignedEmpty()){
				medicalRecords = medicalRecordPutAwayService.getMedicalRecordPutAway(params);
				count= medicalRecordPutAwayService.getMedicalRecordCountPutAway(params);
				//medicalRecords = medicalRecordPutAwayService.getMedicalRecordOfPutAway(params);
				//count= medicalRecordPutAwayService.getMedicalRecordCountOfPutAway(params);
			}
			
			params.setTotalCounts(count);
			params.setQueryDatas(medicalRecords);
			respondResult = new RespondResult(true, RespondResult.successCode, null, params);
		}catch (Exception e) {
			// TODO: handle exception
			respondResult = new RespondResult(false, RespondResult.errorCode, e.getMessage(), params);
		}
		
		return respondResult;
	}
	
	
	
	@GetMapping("/export_put_away_xls")
	public void exportPutAwayXLS(MedicalRecordQuery params,String beyondDay,HttpServletResponse response) {
		OutputStream out = null;
		try{
			List<Map<String,Object>> medicalRecords = new ArrayList<Map<String,Object>>();
			if(!params.querySignedEmpty()){
				params.setPageSize(null);
				medicalRecords = medicalRecordPutAwayService.getMedicalRecordOfPutAway(params);
			}
			response.setContentType("octets/stream");
			response.setCharacterEncoding("GBK");
			response.setContentType("applicationnd.ms-excel");
			String title= "病案归档"+DateUtil.dateFormat(new Date());
			StringBuilder downLoadFileName = new StringBuilder();
			downLoadFileName = new StringBuilder(
					"attachment;filename=");
			downLoadFileName.append(title)
					.append(".xls");
			/*downLoadFileName.append(title).append(DateUtil.dateFormat(new Date()))
			.append(".dbf");*/
			String encodeStr = new String(downLoadFileName.toString().getBytes(
					"GB2312"), "ISO_8859_1");
			response.addHeader("Content-Disposition", encodeStr);
			out = response.getOutputStream();
			List<String> headers = new ArrayList<String>();
			headers.add("住院号");
			headers.add("姓名");
			headers.add("科别");
			headers.add("出院日期");
			headers.add("归档日期");
			List<String> keys = new ArrayList<String>();
			keys.add("only_id");
			keys.add("patient_name");
			keys.add("out_dept_name");
			keys.add("out_hospital_date");
			keys.add("trace_date_format");
			List<Map<String, String>> records = new ArrayList<Map<String,String>>();
			for (Map<String, Object> medicalRecord : medicalRecords) {
				Map<String, String> record = new HashMap<String, String>();
				record.put("only_id",(String)medicalRecord.get("only_id"));
				record.put("patient_name",(String)medicalRecord.get("patient_name"));
				record.put("out_dept_name",(String)medicalRecord.get("out_dept_name"));
				record.put("out_hospital_date",(String)medicalRecord.get("out_hospital_date"));
				record.put("trace_date_format",(String)medicalRecord.get("trace_date_format"));
				records.add(record);
			}
			ExcelUtils.exportExcel(out, title, headers, keys, records);
			
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			if(out != null){
				try {
					out.flush();
				} catch (Exception e2) {
					// TODO: handle exception
				}
				try {
					out.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
			
		}
		
	}
	
	
	
}
